

// 配置新的路由接口

var express = require("express")
var router = express.Router()
var { vuser_model, vxueke_model, vbanji_model, item_model } = require("../utils/model")
var { createToken, checkToken } = require("../utils/token")

router.get("/test", (req, res) => {
    res.send("这是 Vue 项目的 测试接口 --- test -test ")
})


// 注册  
router.post("/xueyuanadd", (req, res) => {
    var body = req.body
    vuser_model.findOne({
        phone: body.phone
    })
        .then(result => {
            if (result) {
                res.json({
                    code: 401,   // 401 和 403 
                    msg: "当前的学员手机号码已经被注册",
                    result
                })
            } else {
                body.time = new Date();
                body.role = 0;  // 学员注册 
                vuser_model.insertMany(body)
                    .then(data => {
                        res.json({
                            code: 200,
                            msg: "注册成功",
                            result: data
                        })
                    })
            }
        })
        .catch(err => {
            console.log(err)
            // res.json()
            // res.render 
            res.json({
                code: 500,
                msg: "服务器异常",
                err
            })
        })
})

// 登录
router.post("/login", (req, res) => {
    var body = req.body
    vuser_model.findOne({
        phone: body.phone,
    })
        .then(result => {
            if (result) {
                if (result.pass == body.pass) {
                    // token 
                    var token = createToken(body)
                    res.json({
                        code: 200,
                        msg: "登录成功",
                        result,
                        token
                    })
                } else {
                    res.json({
                        code: 401,
                        msg: "登录失败",
                        result
                    })
                }
            } else {
                res.json({
                    code: 401,
                    msg: "当前用户未注册,请联系管理员或者注册",
                    result
                })
            }
        })
        .catch(err => {
            console.log(err)
            res.json({
                code: 500,
                msg: "服务器异常",
                err
            })
        })
})

// 获取用户信息 
router.get("/getuserinfo", (req, res) => {
    // token 获取用户信息  
    // phone 
    checkToken(req, res, ({ phone }) => {   // token  校验成功 
        vuser_model.findOne({
            phone
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "获取用户信息成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})


// 上传文件
var multer = require("multer")
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './public/avatar') // 新建 avatar
    },
    filename: function (req, file, cb) {
        cb(null, 'WH2105-' + Date.now() + file.originalname)  // 避免命名冲突  1.jpg  WH2105-123493292191.jpg
    }
})

var upload = multer({ storage: storage }).any()   // 不限文件格式  不限数据 word json 

router.post("/uploadavatar", upload, (req, res) => {
    console.log(req.files)

    var path = req.files[0].path
    checkToken(req, res, ({ phone }) => {
        // 修改 用户数据
        vuser_model.updateMany({
            phone
        }, {
            $set: {
                avatar: path
            }
        }).then(result => {
            res.json({
                code: 200,
                msg: "上传成功",
                path: path,
                result
            })
        })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})


// 添加学科

router.post("/addxueke", (req, res) => {
    var body = req.body;
    checkToken(req, res, ({ phone }) => {
        vxueke_model.findOne({
            value: body.value
        })
            .then(result => {
                if (result) {
                    res.json({
                        code: 401,
                        msg: "当前学科已经被添加",
                        result
                    })
                } else {
                    vxueke_model.insertMany(body)
                        .then(data => {
                            res.json({
                                code: 200,
                                msg: "学科添加成功",
                                result: data
                            })
                        })
                }
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

// 获取学科
router.post("/getxueke", (req, res) => {
    // 搜索
    var body = req.body;
    var obj = {}
    var keyword = body.keyword
    if (keyword) {
        obj = {
            $or: [
                {
                    value: new RegExp(keyword)   // 模糊搜索 
                },
                {
                    text: new RegExp(keyword)
                }
            ]
        }
    }
    checkToken(req, res, ({ phone }) => {
        vxueke_model.find(obj)
            .then(result => {
                res.json({
                    code: 200,
                    msg: "获取学科成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

// 删除学科
router.post("/delxueke", (req, res) => {
    var body = req.body;
    checkToken(req, res, ({ phone }) => {
        vxueke_model.remove({
            _id: body._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "删除学科成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})


// 修改学科
router.post("/updatexueke", (req, res) => {
    var body = req.body;
    checkToken(req, res, ({ phone }) => {
        vxueke_model.updateMany({
            _id: body._id
        }, {
            $set: body
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "修改学科成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})


// 添加班级
router.post("/addbanji", (req, res) => {
    var body = req.body
    const {
        xueke,
        year,
        num
    } = body
    console.log(body)
    checkToken(req, res, ({ phone }) => {
        vbanji_model.findOne({
            xueke,
            year,
            num
        })
            .then(data => {
                if (data) {
                    res.json({
                        code: 401,
                        msg: "班级已存在",
                    })
                } else {
                    vxueke_model.findOne({
                        value: xueke
                    })
                        .then(val => {
                            console.log(val)
                            vbanji_model.insertMany({
                                year,
                                num,
                                xueke,
                                value: xueke + year + num,
                                text: "WH-" + val.text + '-' + year + num
                            })
                                .then(result => {
                                    res.json({
                                        code: 200,
                                        msg: "班级添加成功",
                                        result
                                    })
                                })
                        })
                }
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

// 查询班级
router.post("/getbanjis", (req, res) => {
    // 搜索
    var body = req.body;
    var obj = {}
    var keyword = body.keyword
    if (keyword) {
        obj = {
            $or: [
                {
                    value: new RegExp(keyword)   // 模糊搜索 
                },
                {
                    text: new RegExp(keyword)
                }
            ]
        }
    }
    checkToken(req, res, ({ phone }) => {
        vbanji_model.find(obj)
            .sort({
                year: 1,
                num: 1
            })    // 年份 升序  num 升序
            .then(result => {
                res.json({
                    code: 200,
                    msg: "获取班级成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

// 删除班级
router.post("/delbanji", (req, res) => {
    var body = req.body;
    checkToken(req, res, ({ phone }) => {
        vbanji_model.remove({
            _id: body._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "删除班级成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})


// 添加用户  
router.post("/useradd", (req, res) => {
    var body = req.body
    checkToken(req, res, ({ phone }) => {
        vuser_model.findOne({
            phone: body.phone
        })
            .then(result => {
                if (result) {
                    res.json({
                        code: 401,   // 401 和 403 
                        msg: "当前的用户被已经被注册",
                        result
                    })
                } else {
                    body.time = new Date();
                    vuser_model.insertMany(body)
                        .then(data => {
                            res.json({
                                code: 200,
                                msg: "添加成功",
                                result: data
                            })
                        })
                }
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})


// 查询班级
router.post("/getuserlist", (req, res) => {
    // 搜索
    var body = req.body;
    var obj = {}
    var keyword = body.keyword
    var role = body.role  // '0'   状态 0   
    if (keyword || role || role == '0') {
        obj = {
            username: new RegExp(keyword),    // 模糊搜索 
            role: role
        }
    }
    console.log(obj)
    checkToken(req, res, ({ phone }) => {
        vuser_model.find(obj)
            .sort({
                _id: -1
            })    // 年份 升序  num 升序
            .then(result => {
                res.json({
                    code: 200,
                    msg: "获取用户成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})


// 删除用户
router.post("/deluserone", (req, res) => {
    var body = req.body;
    checkToken(req, res, ({ phone }) => {
        vuser_model.remove({
            _id: body._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "删除用户成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//获取客户信息
router.get("/getuserinfo", (req, res) => {
    //token 获取用户信息
    //phone
    checkToken(req, res, ({ phone }) => {  //token 校验成功
        vuser_model.findOne({
            phone
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "获取用户信息成功",
                    result,
                })
            })
            .catch(err => {
                console.log(err);
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//获取物品
router.post("/getItemList", (req, res) => {
    var body = req.body;
    console.log(body);
    var pagenum = req.body.pagenum
    var pagesize = req.body.pagesize
    item_model.find({})
        .then(result => {
            res.json({
                code: 200,
                msg: "获取成功",
                result
            })
        })
        .catch(err => {
            console.log(err)
            res.json({
                code: 500,
                msg: "服务器异常",
                err
            })
        })
})

//删除物品
router.post("/deleteItem", (req, res) => {
    var body = req.body;
    item_model.remove({
        _id: body._id
    })
        .then(result => {
            res.json({
                code: 200,
                msg: "删除装备成功",
                result
            })
        })
        .catch(err => {
            console.log(err)
            res.json({
                code: 500,
                msg: "服务器异常",
                err
            })
        })
})


module.exports = router

